package com.structure.project.common.utils.file;

import cn.hutool.core.io.FileTypeUtil;
import com.structure.project.common.error.ExceptionFactory;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author jh
 * @date 2023/02/28 10:16
 */
public class FileTypeUtils {

    private final static String IMAGE_TYPE = "image/";

    private final static String AUDIO_TYPE = "audio/";

    private final static String VIDEO_TYPE = "video/";

    private final static String APPLICATION_TYPE = "application/";

    private final static String TXT_TYPE = "text/";

    public static String getFileType(MultipartFile multipartFile) {
        InputStream inputStream = null;
        String type = null;
        try {
            inputStream = multipartFile.getInputStream();
            type = getType(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getFileType(InputStream inputStream) {
        return getType(inputStream);
    }

    public static String getFileType(byte[] bytes) {
        return getType(new ByteArrayInputStream(bytes));
    }

    private static String getType(InputStream inputStream) {

        String type = FileTypeUtil.getType(inputStream);
        if (type.equalsIgnoreCase("JPG") || type.equalsIgnoreCase("JPEG")
                || type.equalsIgnoreCase("GIF") || type.equalsIgnoreCase("PNG")
                || type.equalsIgnoreCase("BMP") || type.equalsIgnoreCase("PCX")
                || type.equalsIgnoreCase("TGA") || type.equalsIgnoreCase("PSD")
                || type.equalsIgnoreCase("TIFF")) {
            return IMAGE_TYPE + type;
        }
        if (type.equalsIgnoreCase("mp3") || type.equalsIgnoreCase("OGG")
                || type.equalsIgnoreCase("WAV") || type.equalsIgnoreCase("REAL")
                || type.equalsIgnoreCase("APE") || type.equalsIgnoreCase("MODULE")
                || type.equalsIgnoreCase("MIDI") || type.equalsIgnoreCase("VQF")
                || type.equalsIgnoreCase("CD")) {
            return AUDIO_TYPE + type;
        }
        if (type.equalsIgnoreCase("mp4") || type.equalsIgnoreCase("avi")
                || type.equalsIgnoreCase("MPEG-1") || type.equalsIgnoreCase("RM")
                || type.equalsIgnoreCase("ASF") || type.equalsIgnoreCase("WMV")
                || type.equalsIgnoreCase("qlv") || type.equalsIgnoreCase("MPEG-2")
                || type.equalsIgnoreCase("MPEG4") || type.equalsIgnoreCase("mov")
                || type.equalsIgnoreCase("3gp")) {
            return VIDEO_TYPE + type;
        }
//        if (type.equalsIgnoreCase("doc") || type.equalsIgnoreCase("docx")
//                || type.equalsIgnoreCase("ppt") || type.equalsIgnoreCase("pptx")
//                || type.equalsIgnoreCase("xls") || type.equalsIgnoreCase("xlsx")
//                || type.equalsIgnoreCase("zip") || type.equalsIgnoreCase("jar")) {
//            return APPLICATION_TYPE + type;
//        }
        if (type.equalsIgnoreCase("txt")) {
            return TXT_TYPE + type;
        }
        return APPLICATION_TYPE + type;
    }

    /**
     * 生成固定FileDto
     *
     * @param file file
     * @return {@link FileDTO}
     * @throws IOException 异常
     */
    public static FileDTO returnFileDTO(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(originalFilename);
        try {
            return FileDTO.builder()
                    .file(file.getInputStream())
                    .fileOriginalName(file.getOriginalFilename())
                    .fileExtName(extension)
                    .build();
        } catch (Exception e) {
            throw ExceptionFactory.instanceServerException("fileDto生成失败", e);
        }
    }

    /**
     * 获取文件类型
     * <p>
     * 例如: ruoyi.txt, 返回: txt
     *
     * @param fileName 文件名
     * @return 后缀（不含".")
     */
    public static String getFileType(String fileName) {
        int separatorIndex = fileName.lastIndexOf(".");
        if (separatorIndex < 0) {
            return "";
        }
        return fileName.substring(separatorIndex + 1).toLowerCase();
    }

    /**
     * 获取文件类型
     *
     * @param photoByte 文件字节码
     * @return 后缀（不含".")
     */
    public static String getFileExtendName(byte[] photoByte) {
        String strFileExtendName = "JPG";
        if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
                && ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97)) {
            strFileExtendName = "GIF";
        } else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70)) {
            strFileExtendName = "JPG";
        } else if ((photoByte[0] == 66) && (photoByte[1] == 77)) {
            strFileExtendName = "BMP";
        } else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71)) {
            strFileExtendName = "PNG";
        }
        return strFileExtendName;
    }
}

